package com.example.bgmachine.bgmachine.clearpopups;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.bgmachine.bgmachine.R;
import com.example.bgmachine.bgmachine.exts.CleanRecordData;
import com.example.bgmachine.bgmachine.exts.SlovmeData;
import com.example.bgmachine.bgmachine.handlouts.HandlContentAdapter;
import com.example.bgmachine.bgmachine.homes.HomePages;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

public class ClearpupView extends Dialog implements
        View.OnClickListener {
    Context context;

    private String TAG="ClearpupView";

    private Boolean bool;


    private Button clear_view_select_dialog_start_btn = null;
    private Button clear_view_select_dialog_stop_btn = null;
    private Button clear_view_select_dialog_cancel_btn = null;


    private TextView clear_view_select_dialog_top_progress_textview = null;

    private LinearLayout clear_progress_one = null;
    private TextView clear_progress_textview_one = null;
    private LinearLayout clear_progress_ones = null;
    private TextView clear_progress_textview_ones = null;

    private RelativeLayout clear_progress_two = null;
    private TextView clear_progress_textview_two = null;
    private RelativeLayout clear_progress_twos = null;
    private TextView clear_progress_textview_twos = null;


    private RelativeLayout clear_progress_three = null;
    private TextView clear_progress_textview_three = null;
    private RelativeLayout clear_progress_threes = null;
    private TextView clear_progress_textview_threes = null;


    private RelativeLayout clear_progress_four = null;
    private TextView clear_progress_textview_four = null;
    private RelativeLayout clear_progress_fours = null;
    private TextView clear_progress_textview_fours = null;

    private RelativeLayout clear_progress_five = null;
    private TextView clear_progress_textview_five = null;
    private RelativeLayout clear_progress_fives = null;
    private TextView clear_progress_textview_fives = null;

    private RelativeLayout clear_progress_six = null;
    private TextView clear_progress_textview_six = null;
    private RelativeLayout clear_progress_sixs = null;
    private TextView clear_progress_textview_sixs = null;


    private ListView clear_view_clear_listvew = null;
    private List<ClearpupData> mClearpupData = null;
    private ClearpupAdapter mClearpupAdapter = null;


    private int mMaxIndex = 0;
    private int mSelectIndex = 0;
    private int mMaxAllTimes = 100;
    private int mCurrentTimes = 0;
    private int mTempTimes = 0;
    private int mprogressNumbers = 0;

    private int mcurrentIndex = 0;
    private int mprogressIndexNumbers = 0;
    private int mTempIndexNumbers = 0;
    private HashMap<Integer, Integer> mIndexTimes = new HashMap<Integer, Integer>();
    private HashMap<Integer, Integer> mIndexLens = new HashMap<Integer, Integer>();
    private HashMap<Integer, ArrayList<ClearItem>> mClearLens = new HashMap<Integer,  ArrayList<ClearItem>>();

    private View.OnClickListener btnOnClick;
    private boolean mIsRuns = false;

    private Handler mTimeTestOne = new Handler() {
        public void handleMessage(android.os.Message msg) {
            mTimeTestOne.removeMessages(101);
            //  mUserDataThread.start();
            if (mIsRuns) {
                mCurrentTimes++;
                mTempTimes++;

                mTimeTestOne.sendEmptyMessageDelayed(101, 1000);


                handProgress();

            }


        }
    };

    /**
     * @param @param btnOnClick
     * @return void
     * @throws
     * @Description: 可以自定义button事件
     * @author Sunday
     */
    public void setBtnOnClick(View.OnClickListener btnOnClick) {
        this.btnOnClick = btnOnClick;

    }

    public void setBtnOkClick(String text,
                              View.OnClickListener btnOnClick) {
        this.btnOnClick = btnOnClick;

    }

    public ClearpupView(Context context) {
        super(context, R.style.SundayDialogs);
        this.context = context;
        bool = true;


        Log.d("aaa", "aaa===2");
        //create();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.clear_view_select_dialog);
        init();
        //  setCancel(bool, "");

        Log.d("aaa", "aaa===1");
    }

    void init() {

        clear_view_select_dialog_start_btn = findViewById(R.id.clear_view_select_dialog_start_btn);
        clear_view_select_dialog_start_btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (clear_view_select_dialog_start_btn != null) {
                    clear_view_select_dialog_start_btn.setVisibility(Button.GONE);
                }
                if (clear_view_select_dialog_stop_btn != null) {
                    clear_view_select_dialog_stop_btn.setVisibility(Button.VISIBLE);
                }
                mTempIndexNumbers = 0;
                mprogressIndexNumbers = 0;
                mprogressNumbers = 0;
                mCurrentTimes = 0;
                mTempTimes = 0;
                mcurrentIndex = 0;
                mIsRuns = true;
               // mTimeTestOne.sendEmptyMessageDelayed(101, 1000);
                long currentTime = System.currentTimeMillis();
                long currentTimes = currentTime / 1000;
                String timeNow = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(currentTime);
                String ordernums = String.valueOf(currentTime);
                CleanRecordData.getCleanRecordData().setBeginTime(String.valueOf(currentTimes));
                CleanRecordData.getCleanRecordData().setOrdnumbers(ordernums);
                CleanRecordData.getCleanRecordData().setBeginEnd(String.valueOf(currentTimes));
                CleanRecordData.getCleanRecordData().setRealTime(String.valueOf(mMaxAllTimes));
                sendClearCmd();
               HomePages.mHomePages.sendRequestRealClean();
            }
        });

        clear_view_select_dialog_stop_btn = findViewById(R.id.clear_view_select_dialog_stop_btn);
        clear_view_select_dialog_stop_btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if(!mIsRuns){

                    Toast.makeText(context, context.getResources().getString(R.string.clearup_view_stop_texts) , Toast.LENGTH_SHORT).show();
                    return;
                }
                mIsRuns = false;
                mTimeTestOne.removeMessages(101);
                if (clear_view_select_dialog_stop_btn != null) {
                    clear_view_select_dialog_stop_btn.setVisibility(Button.GONE);
                }
                if (clear_view_select_dialog_start_btn != null) {
                    clear_view_select_dialog_start_btn.setVisibility(Button.VISIBLE);
                }

                dismiss();
                long currentTime = System.currentTimeMillis();
                long currentTimes = currentTime / 1000;
                CleanRecordData.getCleanRecordData().setBeginEnd(String.valueOf(currentTimes));
                HomePages.mHomePages.cancelOut();

                HomePages.mHomePages.sendRequestEndRealClean();
            }
        });
        clear_view_select_dialog_cancel_btn = findViewById(R.id.clear_view_select_dialog_cancel_btn);
        clear_view_select_dialog_cancel_btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if(mIsRuns){

                    Toast.makeText(context, context.getResources().getString(R.string.clearup_view_run_texts) , Toast.LENGTH_SHORT).show();
                    return;
                }
                mIsRuns = false;
                mTimeTestOne.removeMessages(101);
                dismiss();
            }
        });

        clear_view_select_dialog_top_progress_textview = findViewById(R.id.clear_view_select_dialog_top_progress_textview);


        clear_progress_one = findViewById(R.id.clear_progress_one);
        clear_progress_textview_one = findViewById(R.id.clear_progress_textview_one);
        clear_progress_ones = findViewById(R.id.clear_progress_ones);
        clear_progress_textview_ones = findViewById(R.id.clear_progress_textview_ones);

        clear_progress_two = findViewById(R.id.clear_progress_two);
        clear_progress_textview_two = findViewById(R.id.clear_progress_textview_two);
        clear_progress_twos = findViewById(R.id.clear_progress_twos);
        clear_progress_textview_twos = findViewById(R.id.clear_progress_textview_twos);


        clear_progress_three = findViewById(R.id.clear_progress_three);
        clear_progress_textview_three = findViewById(R.id.clear_progress_textview_three);
        clear_progress_threes = findViewById(R.id.clear_progress_threes);
        clear_progress_textview_threes = findViewById(R.id.clear_progress_textview_threes);


        clear_progress_four = findViewById(R.id.clear_progress_four);
        clear_progress_textview_four = findViewById(R.id.clear_progress_textview_four);
        clear_progress_fours = findViewById(R.id.clear_progress_fours);
        clear_progress_textview_fours = findViewById(R.id.clear_progress_textview_fours);

        clear_progress_five = findViewById(R.id.clear_progress_five);
        clear_progress_textview_five = findViewById(R.id.clear_progress_textview_five);
        clear_progress_fives = findViewById(R.id.clear_progress_fives);
        clear_progress_textview_fives = findViewById(R.id.clear_progress_textview_fives);

        clear_progress_six = findViewById(R.id.clear_progress_six);
        clear_progress_textview_six = findViewById(R.id.clear_progress_textview_six);
        clear_progress_sixs = findViewById(R.id.clear_progress_sixs);
        clear_progress_textview_sixs = findViewById(R.id.clear_progress_textview_sixs);


        clear_view_clear_listvew = findViewById(R.id.clear_view_clear_listvew);
        clear_view_clear_listvew.setDivider(null);
        mClearpupData = new LinkedList<ClearpupData>();
        mClearpupAdapter = new ClearpupAdapter(context, (LinkedList<ClearpupData>) mClearpupData);
        if (clear_view_clear_listvew != null) {
            clear_view_clear_listvew.setAdapter(mClearpupAdapter);
        }

    }

    public void handProgress() {
        if (clear_view_select_dialog_top_progress_textview != null) {

            if (mMaxAllTimes <= 0) {
                mMaxAllTimes = 1;
            }
            if (mClearpupData != null) {
                if (mprogressNumbers < mClearpupData.size()) {
                    mClearpupData.get(mprogressNumbers).setCurrentProgress(mTempTimes);
                    Log.d(TAG, "handProgress: mprogressNumbers=="+mprogressNumbers);

                    clear_view_clear_listvew.smoothScrollToPosition(mprogressNumbers);

                }
            }
            if (mClearpupAdapter != null) {
                mClearpupAdapter.notifyDataSetChanged();
            }
            if (mprogressNumbers < mIndexTimes.size()) {
                int lenas = mIndexTimes.get(mprogressNumbers);
                if (mTempTimes >= lenas) {
                    mprogressNumbers++;
                    mTempTimes = 0;
                    mTempIndexNumbers++;
                    if (mprogressIndexNumbers < mIndexLens.size()) {
                        int lenbs = mIndexLens.get(mprogressIndexNumbers);
                        Log.d(TAG, "handProgress: mprogressIndexNumbers=="+mprogressIndexNumbers);
                        Log.d(TAG, "handProgress: lenbs=="+lenbs);

                        if (mTempIndexNumbers >= lenbs) {
                            mprogressIndexNumbers++;
                            mTempIndexNumbers = 0;
                            setSelectIndex(mprogressIndexNumbers);
                            pause();
                        }
                    }


                }
            }


            clear_view_select_dialog_top_progress_textview.setText(String.valueOf(mCurrentTimes * 100 / mMaxAllTimes) + "%");
            if (mCurrentTimes >= mMaxAllTimes) {
                mIsRuns = false;


            }
        }

    }
    public void pause(){
        mTimeTestOne.removeMessages(101);
    }
    public void sendClearCmd(){
        if(!mIsRuns){
            return;
        }

        if(mprogressIndexNumbers < mClearLens.size() ){
            Log.d(TAG, "sendClearCmd:mprogressIndexNumbers="+mprogressIndexNumbers);
            ArrayList<ClearItem> lista =  mClearLens.get(mprogressIndexNumbers);
            mTimeTestOne.sendEmptyMessageDelayed(101, 1000);

            HomePages.mHomePages.clearCmd(lista);
        }else {
            long currentTime = System.currentTimeMillis();
            long currentTimes = currentTime / 1000;
            CleanRecordData.getCleanRecordData().setBeginEnd(String.valueOf(currentTimes));
            HomePages.mHomePages.sendRequestEndRealClean();
        }

    }

    public void initView(String infors) {
        mClearpupData.clear();
        if (clear_view_select_dialog_stop_btn != null) {
            clear_view_select_dialog_stop_btn.setVisibility(Button.GONE);
        }
        if (clear_view_select_dialog_start_btn != null) {
            clear_view_select_dialog_start_btn.setVisibility(Button.VISIBLE);
        }

        if (clear_view_select_dialog_top_progress_textview != null) {
            clear_view_select_dialog_top_progress_textview.setText("0%");
        }

        int maxLine = 0;
        mMaxAllTimes = 0;
        int nums = 0;
        int numa = 0;
        mprogressNumbers = 0;
        mcurrentIndex = 0;
        mprogressIndexNumbers = 0;
        mTempIndexNumbers = 0;
        try {
            JSONArray arrys = new JSONArray(infors);
            maxLine = 0;//arrys.length();
            for (int i = 0; i < arrys.length(); i++) {
                JSONObject jsona = arrys.getJSONObject(i);
                JSONArray lists = jsona.optJSONArray("children");
                if( lists.length() <= 1){
                    continue;
                }
                maxLine++;
                String names = jsona.opt("cleantime_name").toString();
                setIndename(i + 1, names);

                mIndexLens.put(numa, lists.length() - 1);

                ArrayList<ClearItem> arrLisa=new   ArrayList<ClearItem>();
                for (int j = 0; j < lists.length(); j++) {
                    JSONObject jsonb = lists.getJSONObject(j);
                    Log.d(TAG, "initView: jsonb=="+jsonb.toString());
                    int is_pines_status = Integer.parseInt(jsonb.optString("is_pines_status") );
                    String veer = jsonb.optString("veer");
                    Log.d(TAG, "initView: veer=="+veer);
                    if(is_pines_status == 1){
                        String rev = jsonb.optString("rev");
                        String pins = jsonb.optString("rulepines");
                        if(veer=="2"){
                            veer="0";
                        }
                        arrLisa.add (0,new ClearItem(String.valueOf(is_pines_status),pins,veer,rev, "0") );

                        continue;
                    }

                    int index = Integer.parseInt(jsonb.optString("rulepines") );
                    String namaes = jsonb.opt("rulename").toString();
                    int times =  Integer.parseInt( jsonb.optString("duration") );
                    if(veer=="2"){
                        veer="0";
                    }
                    arrLisa.add(new ClearItem(String.valueOf(is_pines_status),String.valueOf(index),veer,"0",String.valueOf(times)) );
                    mMaxAllTimes += times;
                    mIndexTimes.put(nums, times);
                    mClearpupData.add(new ClearpupData(index, namaes, 0, times));
                    nums++;

                }
                mClearLens.put(numa,arrLisa);
                numa++;
            }
        } catch (JSONException e) {
            e.printStackTrace();

        }
        setMaxIndex(maxLine);
        setSelectIndex(0);
    }

    public void setIndename(int index, String names) {
        switch (index) {
            case 1:
                if (clear_progress_textview_one != null) {
                    clear_progress_textview_one.setText(names);
                }
                if (clear_progress_textview_ones != null) {
                    clear_progress_textview_ones.setText(names);
                }

                break;
            case 2:
                if (clear_progress_textview_two != null) {
                    clear_progress_textview_two.setText(names);
                }
                if (clear_progress_textview_twos != null) {
                    clear_progress_textview_twos.setText(names);
                }

                break;
            case 3:
                if (clear_progress_textview_three != null) {
                    clear_progress_textview_three.setText(names);
                }
                if (clear_progress_textview_threes != null) {
                    clear_progress_textview_threes.setText(names);
                }

                break;
            case 4:
                if (clear_progress_textview_four != null) {
                    clear_progress_textview_four.setText(names);
                }
                if (clear_progress_textview_fours != null) {
                    clear_progress_textview_fours.setText(names);
                }
                break;
            case 5:
                if (clear_progress_textview_five != null) {
                    clear_progress_textview_five.setText(names);
                }
                if (clear_progress_textview_fives != null) {
                    clear_progress_textview_fives.setText(names);
                }
                break;
            case 6:
                if (clear_progress_textview_six != null) {
                    clear_progress_textview_six.setText(names);
                }
                if (clear_progress_textview_sixs != null) {
                    clear_progress_textview_sixs.setText(names);
                }
                break;
            default:
                break;

        }
    }

    public void setSelectIndex(int index) {
        mSelectIndex = index;
        Log.d(TAG, "setSelectIndex:index="+index);
        if (clear_progress_one != null) {
            if (mSelectIndex >= 1) {
                clear_progress_one.setVisibility(RelativeLayout.GONE);

            } else {
                if (mMaxIndex >= 1) {
                    clear_progress_one.setVisibility(RelativeLayout.VISIBLE);
                }


            }
        }

        if (clear_progress_ones != null) {

            if (mSelectIndex >= 1) {
                clear_progress_ones.setVisibility(RelativeLayout.VISIBLE);

            } else {
                clear_progress_ones.setVisibility(RelativeLayout.GONE);

            }
        }


        if (clear_progress_two != null) {

            if (mSelectIndex >= 2) {
                clear_progress_two.setVisibility(RelativeLayout.GONE);

            } else {
                if (mMaxIndex >= 2) {
                    clear_progress_two.setVisibility(RelativeLayout.VISIBLE);
                }
            }
        }

        if (clear_progress_twos != null) {

            if (mSelectIndex >= 2) {
                clear_progress_twos.setVisibility(RelativeLayout.VISIBLE);

            } else {
                clear_progress_twos.setVisibility(RelativeLayout.GONE);

            }
        }


        if (clear_progress_three != null) {

            if (mSelectIndex >= 3) {
                clear_progress_three.setVisibility(RelativeLayout.GONE);

            } else {
                if (mMaxIndex >= 3) {
                    clear_progress_three.setVisibility(RelativeLayout.VISIBLE);
                }
            }
        }

        if (clear_progress_threes != null) {

            if (mSelectIndex >= 3) {
                clear_progress_threes.setVisibility(RelativeLayout.VISIBLE);

            } else {
                clear_progress_threes.setVisibility(RelativeLayout.GONE);

            }
        }


        if (clear_progress_four != null) {

            if (mSelectIndex >= 4) {
                clear_progress_four.setVisibility(RelativeLayout.GONE);

            } else {
                if (mMaxIndex >= 4) {
                    clear_progress_four.setVisibility(RelativeLayout.VISIBLE);
                }
            }
        }

        if (clear_progress_fours != null) {

            if (mSelectIndex >= 4) {
                clear_progress_fours.setVisibility(RelativeLayout.VISIBLE);

            } else {
                clear_progress_fours.setVisibility(RelativeLayout.GONE);

            }
        }


        if (clear_progress_five != null) {

            if (mSelectIndex >= 5) {
                clear_progress_five.setVisibility(RelativeLayout.GONE);

            } else {
                if (mMaxIndex >= 5) {
                    clear_progress_five.setVisibility(RelativeLayout.VISIBLE);
                }
            }
        }

        if (clear_progress_fives != null) {

            if (mSelectIndex >= 5) {
                clear_progress_fives.setVisibility(RelativeLayout.VISIBLE);

            } else {
                clear_progress_fives.setVisibility(RelativeLayout.GONE);

            }
        }


        if (clear_progress_six != null) {

            if (mSelectIndex >= 6) {
                clear_progress_six.setVisibility(RelativeLayout.GONE);

            } else {
                if (mMaxIndex >= 6) {
                    clear_progress_six.setVisibility(RelativeLayout.VISIBLE);
                }
            }
        }

        if (clear_progress_sixs != null) {

            if (mSelectIndex >= 6) {
                clear_progress_sixs.setVisibility(RelativeLayout.VISIBLE);

            } else {
                clear_progress_sixs.setVisibility(RelativeLayout.GONE);

            }
        }
    }

    public void setMaxIndex(int index) {
        mMaxIndex = index;

        if (clear_progress_one != null) {
            if (mMaxIndex >= 1) {
                clear_progress_one.setVisibility(RelativeLayout.VISIBLE);

            } else {
                clear_progress_one.setVisibility(RelativeLayout.GONE);

            }
        }

        if (clear_progress_ones != null) {
            clear_progress_ones.setVisibility(RelativeLayout.GONE);
        }


        if (clear_progress_two != null) {

            if (mMaxIndex >= 2) {
                clear_progress_two.setVisibility(RelativeLayout.VISIBLE);

            } else {
                clear_progress_two.setVisibility(RelativeLayout.GONE);

            }
        }

        if (clear_progress_twos != null) {
            clear_progress_twos.setVisibility(RelativeLayout.GONE);
        }


        if (clear_progress_three != null) {
            clear_progress_three.setVisibility(RelativeLayout.VISIBLE);
            if (mMaxIndex >= 3) {
                clear_progress_three.setVisibility(RelativeLayout.VISIBLE);

            } else {
                clear_progress_three.setVisibility(RelativeLayout.GONE);

            }
        }

        if (clear_progress_threes != null) {
            clear_progress_threes.setVisibility(RelativeLayout.GONE);
        }


        if (clear_progress_four != null) {
            clear_progress_four.setVisibility(RelativeLayout.VISIBLE);
            if (mMaxIndex >= 4) {
                clear_progress_four.setVisibility(RelativeLayout.VISIBLE);

            } else {
                clear_progress_four.setVisibility(RelativeLayout.GONE);

            }
        }

        if (clear_progress_fours != null) {
            clear_progress_fours.setVisibility(RelativeLayout.GONE);

        }


        if (clear_progress_five != null) {
            clear_progress_five.setVisibility(RelativeLayout.VISIBLE);
            if (mMaxIndex >= 5) {
                clear_progress_five.setVisibility(RelativeLayout.VISIBLE);

            } else {
                clear_progress_five.setVisibility(RelativeLayout.GONE);

            }
        }

        if (clear_progress_fives != null) {
            clear_progress_fives.setVisibility(RelativeLayout.GONE);
        }


        if (clear_progress_six != null) {
            clear_progress_six.setVisibility(RelativeLayout.VISIBLE);
            if (mMaxIndex >= 6) {
                clear_progress_six.setVisibility(RelativeLayout.VISIBLE);

            } else {
                clear_progress_six.setVisibility(RelativeLayout.GONE);

            }
        }

        if (clear_progress_sixs != null) {
            clear_progress_sixs.setVisibility(RelativeLayout.GONE);
        }


    }

    /*
     *
     */
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        if (isShowing()) {
            //   dismiss();
        }
    }
}
